﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using System.Collections.Generic;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Config;


public partial class Theme_3_MyCourse : PowerCreator.LMS.Web.Base.WebBasePage
{
    ManagerPaper opPaper = new ManagerPaper();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    PowerCreator.LMS.Web.Course.UserCourse opUserCourse = new PowerCreator.LMS.Web.Course.UserCourse();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected string HtmlPublicCourse = "";
    private int pagesize = 6;
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        this.Title = config.GetLanagerValueByKey("theme_Study_MyCourses", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            PowerCreator.LMS.Common.Script.Script.ShowAndCloseMe(this, config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        if (!IsPostBack)
        {
            if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] != null)
            {
                string search = GetSearch();
                HtmlPublicCourse = BinderCourse(search, 1);
                BinderPager(search);
            }
        }
    }




    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }

    protected override void InitPage()
    {
        base.InitPage();
    }
    private string GetSearch()
    {

        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        string search = " CourseState = 1 and";
        search += "  Course_ID in (select FKUCCourseID from UserCourse where UCState=1 and UCUserID=" + login.UserID + " )";
        return search;
    }

    private string BinderCourse(string search, int pageIndex)
    {
        string Html = "";
        //公开课程
        PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
        //string search = "CourseIsCompulsory = 3 and CourseState = 1";
        string orderList = "CourseCreatetime desc,Course_Id desc";
        DataTable dt = opCourse.QueryCourse(search, orderList, pagesize, pageIndex);

        dt.Columns.Add("UserCourseState1"); //选课状态
        dt.Columns.Add("UserCourseState1Name"); //选课状态         ,0审核中，1己审核

        int t = 0;
        if (!PowerCreator.LMS.SCR.Dog.CheckDog())
        {

            for (int i = dt.Rows.Count - 1; i > 1; i--)
            {
                dt.Rows[i].Delete();
                t = t + 1;
            }

        }

        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        for (int i = 0; i < dt.Rows.Count -t; i++)
        {
            search = string.Format(" UCUserID={0} and FKUCCourseID = {1} ", login.UserID, dt.Rows[i]["Course_ID"]);
            //Response.Write(search);
            //Response.End();
            DataTable dtt = opUserCourse.QueryUserCourse(search, "", 1, 1);
            dt.Rows[i]["UserCourseState1"] = "0";
            if (dtt.Rows.Count == 1)
            {
                dt.Rows[i]["UserCourseState1"] = dtt.Rows[0]["UCState"].ToString();
            }
            if (dt.Rows[i]["UserCourseState1"].ToString() == "0")
            {
                dt.Rows[i]["UserCourseState1Name"] = "<font color='red'>" + config.GetLanagerValueByKey("State_Msg3", PowerCreator.LMS.Language.LanagetItemType.Page) + "</font>";
            }
            else if (dt.Rows[i]["UserCourseState1"].ToString() == "1")
            {
                dt.Rows[i]["UserCourseState1Name"] = "";
            }
        }


            for (int i = 0; i < dt.Rows.Count-t; i++)
            {

                Html += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                Html += "<tr>";
                Html += "<td valign=\"top\"  style=\"width:168px;\"><table width=\"168\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
                Html += "<tr>";
                Html += " <td>";
                if (dt.Rows[i]["UserCourseState1"].ToString() == "1")
                    Html += "<a href=\"OpenCourse.aspx?CourseID=" + dt.Rows[i]["Course_ID"].ToString() + "&ProjectID=0\">";
                Html += "<img alt=\"" + dt.Rows[i]["CourseName"].ToString() + "\" src=\"" + dt.Rows[i]["CourseImgUrl"].ToString() + "\" width=\"160\" height=\"120\" style=\"border: 1px solid #99CCFF\" />";
                if (dt.Rows[i]["UserCourseState1"].ToString() == "1")
                    Html += "</a>";

                Html += "</td>";
                Html += "</tr>";
                Html += "</table></td>";

                Html += "<td align=\"left\"><div class=\"center3ct\">";
                Html += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" align=\"center\">";
                Html += "<tr>";
                Html += "<td>";

                if (dt.Rows[i]["UserCourseState1"].ToString() == "1")
                    Html += "<a href=\"OpenCourse.aspx?CourseID=" + dt.Rows[i]["Course_ID"].ToString() + "&ProjectID=0\">";
                Html +=dt.Rows[i]["CourseName"].ToString();
                if (dt.Rows[i]["UserCourseState1"].ToString() == "1")
                    Html += "</a>";
                Html += "&nbsp;&nbsp;" + dt.Rows[i]["UserCourseState1Name"].ToString();
                Html += "</td>";
                Html += "</tr>";
                Html += "<tr>";
                Html += "<td>";
                Html += " <p>" + config.GetLanagerValueByKey("lbl_AddCourseInfo_TeacherName", PowerCreator.LMS.Language.LanagetItemType.Page) + "：" + PowerCreator.LMS.Common.Utils.StringUtils.GetSubstringLue(dt.Rows[i]["CourseMostlyTeacherName"].ToString(), 8) + "</p>";
                Html += "<p> " + config.GetLanagerValueByKey("lbl_AddCourseInfo_CoursePeriod", PowerCreator.LMS.Language.LanagetItemType.Page) + "：" + dt.Rows[i]["CoursePeriod"].ToString() + " </p>";
                Html += "<p> " + PowerCreator.LMS.Common.Utils.StringUtils.GetSubstringLue(dt.Rows[i]["Description"].ToString(),70) + " </p>";

                Html += "</td>";
                Html += " </tr>";

                Html += "</table>";
                Html += "</div></td>";
                Html += "</tr>";
                Html += "</table>";

                Html += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
                Html += "<tr><td height=\"2\"></td></tr>";
                Html += "<tr>";
                Html += "<td class=\"xx1\"></td>";
                Html += " </tr>";
                Html += " <tr><td height=\"2\"></td></tr>";

                Html += " </table>";


            
        }
        return Html;
    }


    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = pagesize;
        AspNetPager1.RecordCount = opCourse.QueryCourseCount(search);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        HtmlPublicCourse = BinderCourse(search, AspNetPager1.CurrentPageIndex);
    }

 }
